﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CSF.Utility.Mvc;
using CSF.Service.Accounts.Client;
using CSF.Customers.Data;
using CSF.Utility.Misc;
namespace CSF.WebApp.Admin.Areas.Common.Controllers
{
    public class TermsController : SecureController
    {
        //
        // GET: /Common/Region/
        [CsfAuthorize]
        public ActionResult Index(bool isDisable = false)
        {
            TermsSvcClient svc = new TermsSvcClient();
            IEnumerable<Terms> list = null;
            if (isDisable)
            {
                list = svc.GetDisable();
            }
            else
            {
                list = svc.GetEnable();
            }
            list = Search(list);
            return View(list);
        }

        [NonAction]
        private IEnumerable<Terms> Search(IEnumerable<Terms> list)
        {
            var collection = Request.QueryString;

            var Code = collection["Code"];
            var name = collection["Name1"];

            if (!string.IsNullOrWhiteSpace(Code))
            {
                list = list.Where(p => Code.InStr(p.Code));
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                list = list.Where(p => name.InStr(new string[] { p.Name1, p.Name2, p.Name3 }));
            }
            return list;
        }
        [CsfAuthorize]
        public ActionResult Add()
        {

            return View();
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Add(Terms model)
        {
            if (ModelState.IsValid)
            {
                model.CreateBy = User.Identity.Name;
                TermsSvcClient svc = new TermsSvcClient();
                svc.Insert(model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public ActionResult Edit(string Code)
        {
            TermsSvcClient svc = new TermsSvcClient();
            Terms model = svc.GetOneByCode(Code);
            return View(model);

        }

        /// <summary>
        /// 编辑记录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Edit(Terms model)
        {
            if (ModelState.IsValid)
            {
                model.ModifyBy = User.Identity.Name;
                TermsSvcClient svc = new TermsSvcClient();
                svc.UpdateByCode(model, model.Code);
                return RedirectToAction("Index");
            }
            return View(model);
        }

        /// <summary>
        /// 删除或禁用记录
        /// </summary>
        /// <param name="id">记录Id集合</param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Delete(IEnumerable<string> Code, string toUrl)
        {
            if (Code != null && Code.Count() > 0)
            {
                TermsSvcClient svc = new TermsSvcClient();
                svc.SetDisable(Code, true, User.Identity.Name);
            }
            return Redirect(toUrl);
        }

    }
}
